package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.szewec.ps.R;
import com.szewec.ps.myinterface.NoDataPageAddData;

public class NoDataPrompt extends ConstraintLayout {
    protected ImageView promptImage;
    protected TextView promptText;
    protected TextView addData;

    public NoDataPrompt(Context context) {
        super(context);
    }

    public NoDataPrompt(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NoDataPrompt(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.no_data_prompt, this);
        promptImage = findViewById(R.id.prompt_img);
        promptText = findViewById(R.id.prompt_text);
        addData = findViewById(R.id.tv_add_data);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.noDataPrompt);
            Drawable img = ta.getDrawable(R.styleable.noDataPrompt_promptImage);
            if(img != null){
                promptImage.setImageDrawable(img);
            }
            String text = ta.getString(R.styleable.noDataPrompt_promptText);
            if(text != null){
                promptText.setText(text);
            }
            ta.recycle();
        }
    }

    public void enableAddData(NoDataPageAddData noDataPageAddData){
        if (addData.getVisibility() == GONE){
            addData.setVisibility(VISIBLE);
        }
        addData.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                noDataPageAddData.linkToAddActivity();
            }
        });
    }

    public void disableAddData(){
        if (addData.getVisibility() == VISIBLE){
            addData.setVisibility(GONE);
        }
    }
}
